/*
 *Class Name: MainWindow
 *Class Function: 实现主界面的UI操作和CreateMatterDialog的new并show。
*/

#include <QTextStream>
#include <QMessageBox>

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "common.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),createMatter(0)
{
    ui->setupUi(this);

    connect(ui->listWidget,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(showItemDetailMessage(QListWidgetItem*)));
    connect(ui->listWidget,SIGNAL(itemDoubleClicked(QListWidgetItem*)),this,SLOT(revampItemMatter(QListWidgetItem*)));
}
MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_CreateMatterButton_clicked()
{
    if(createMatter == 0) {
        cout << "on_CreateMatterButton_clicked" << endl;
        createMatter = new CreateMatterDialog(this);
        connect(createMatter,SIGNAL(destroyed()),this,SLOT(createDialogDone()));//当createMatter对话框释放时，对应的槽
    }
    createMatter->show();
}

//当createMetter对话框释放之后响应这个槽函数
void MainWindow::createDialogDone()
{
    ui->listWidget->UpdateListContent();
    this->createMatter = 0;
}


//响应item的点击事件，然后从参数item中获取到数据并填充到CreateMatter的对话框中
void MainWindow::showItemDetailMessage(QListWidgetItem*item)
{
    if(createMatter == 0) {
        cout << "itemCliectSlot" << endl;
        createMatter = new CreateMatterDialog(this);
        connect(createMatter,SIGNAL(destroyed()),this,SLOT(createDialogDone()));//当createMatter对话框释放时，对应的槽
    }
    createMatter->SetData(item->text(),true);
    createMatter->show();
}


void MainWindow::revampItemMatter(QListWidgetItem *item)
{
    if(createMatter == 0) {
        cout << "revampItemMatter" << endl;
        createMatter = new CreateMatterDialog(this);
        connect(createMatter,SIGNAL(destroyed()),this,SLOT(createDialogDone()));//当createMatter对话框释放时，对应的槽
    }
    createMatter->SetData(item->text(),false);
    createMatter->show();
}
